<?php

defined('_JEXEC') or die();

jimport('joomla.application.component.controller');

require_once(JPATH_COMPONENT.DS.'helpers'.DS.'extensionshelper.php');

class Easiv2ControllerManageExtensions extends JController {
	
	var $db;
	var $extensionsHelper;
	
	function __construct() {
		parent::__construct();
		
		$this->db = JFactory::getDBO();
		$this->extensionsHelper = new ExtensionsHelper('repository');
	}
	
	function getInfo() {
		//$params = xmlrpc_decode($_POST['params']);
		$v = new JVersion();
		$version = $v->getShortVersion();
		
		echo $version;
	}
	
	function getInstallStatus() {
		$param = xmlrpc_decode($_POST['extensions']);
		$status = array();
		
		foreach($param as $extension) {
			$this->db->setQuery('SELECT name, manifest_cache FROM #__extensions WHERE name = "'.$extension.'"');
			$result = $this->db->loadObject();
			
			if(count($result) != 0) {
				// to get to the version info the content of the manifest_cache attribute of the #__extensions table is read.
				$mc = $result->manifest_cache;
			
				/*
				 * the first appearance of the word version is discovered. The result looks like this:
				 * 
				 * version":"1.5.0","description":"It counts the shopping entries","group":""}
				 * 
				 * Next, the position of the ':'-seperator charakter is discovered
				 * 
				 * The result looks like this:
				 */
				$temp = stristr($mc, 'version');
				$seperatorPos = strpos($temp, ':');
				
				/*
				 * Next, the string from the startposition of $temp to the found seperator position is stored in $temp again
				 * 
				 * $temp would now look like this:
				 * 
				 * :"1.5.0","description":"It counts the shopping entries","group":""}
				 */
				$temp = substr($temp, $seperatorPos);
				
				/*
				 * Next, the ','-seperator position is dicovered
				 */
				$seperatorPos = strpos($temp, ',');
				
				/*
				 * Now the version can be read with the following command
				 */
				$version = substr($temp, 2, $seperatorPos-3);
				
				// the answer will be true for installstatus followed by a ':'-seperator and the version number
				//echo "true:".$version;
				$status[$extension] = "true:".$version;
			}
			else {
				$status[$extension] = "false";
			}
		}
		
		echo xmlrpc_encode($status);
	}
	
	function install() {
		$param = xmlrpc_decode($_POST['toInstall']);
		
		$extension = $param[0];
		$identifier = $param[1];
		
		/*
		 * Check, if identifier sent by the management instance is valid
		 */
		$this->db->setQuery("SELECT identifier FROM #__easiv2_mi");
		$thisIdentifier = $this->db->loadObject()->identifier;
		
		if($thisIdentifier == $identifier) {
			if($this->extensionsHelper->install(JPATH_COMPONENT.DS.'repository'.DS.$extension)) {
				echo "true";
				
				JFile::delete(JPATH_COMPONENT.DS.'repository'.DS.$extension);
			}
			else {
				echo "false";
			}
		}
		else {
			echo "identifier";
		}
	}
	
	function uninstall() {
		$param = xmlrpc_decode($_POST['information']);
		
		$extensionName = $param[0];
		$type = $param[1];
		$identifier = $param[2];
		
		$this->db->setQuery("SELECT extension_id FROM #__extensions WHERE name ='".$extensionName."'");		
		$extensionId = $this->db->loadObject()->extension_id;
		
		$this->db->setQuery("SELECT identifier FROM #__easiv2_mi");
		$thisIdentifier = $this->db->loadObject()->identifier;
		
		if($thisIdentifier == $identifier) {
			$installer = new JInstaller();
			
			if(@$installer->uninstall($type, $extensionId, 0)) {
				echo "true";
			}
			else {
				echo "false";
			}
		}
		else {
			echo "false";
		}
	}
}